package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.UserInfoRespository;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.data.domain.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName:UserServiceImp
 * @Description:
 * @Author:Deamer
 * @Date:2020/3/30 20:20
 **/
public class UserServiceImp implements UserService {
    @Resource
    UserInfoRespository userInfoRespository;

    @Override
    public JSONObject getUserInfo(String username, String password) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(password);
        Example<UserInfo> example = Example.of(userInfo);
        List<UserInfo> userInfoList = userInfoRespository.findAll(example);
        JSONObject jsonObject = new JSONObject();
        UserInfo returnUser = null;
        if (userInfoList != null && !userInfoList.isEmpty()) {
            returnUser = userInfoList.get(0);
            jsonObject.put("data", returnUser);
        } else {
            jsonObject.put("code", 1001);
            jsonObject.put("msg", "登录信息错误！");
        }

        return jsonObject;
    }

    @Override
    public Boolean getUsername(String username) {
        String name = userInfoRespository.getUsername(username);
        if (name == null && "".equals(name)) {
            return false;
        }
        return true;
    }
}
